<?php
require_once('./lib/config.php');
header('Content-Type: text/html; charset="UTF-8"');
error_reporting(E_ALL);
$data = false;
$hero = false;
$battleTag = '';
$realm = '';
if (
    (!empty($_COOKIE['realm']) && !empty($_COOKIE['battleTag'])) ||
    (!empty($_REQUEST['realm']) && !empty($_REQUEST['battleTag']))
) {
    if (!empty($_GET['realm'])) {
        $realm = $_GET['realm'];
        $battleTag = $_GET['battleTag'];

        $p = new Parser($_GET['realm'], $_GET['battleTag']);
        setcookie('realm', $_GET['realm']);
        setcookie('battleTag', $_GET['battleTag']);
    } else {
        $p = new Parser($_COOKIE['realm'], $_COOKIE['battleTag']);
        $realm = $_COOKIE['realm'];
        $battleTag = $_COOKIE['battleTag'];
    }

    $data = $p->load();
    if (!empty($data['heroes'][0])) {
        $hero = $p->load(Parser::TYPE_HERO, $data['heroes'][0]['id']);
        if (!empty($hero['items'])) {
            foreach ($hero['items'] as &$item) {
                $item['data'] = $p->load(Parser::TYPE_ITEM, $item['tooltipParams']);
            }
        }
    }
}

?>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="http://dygraphs.com/dygraph-dev.js"></script>
    <script type="text/javascript" src="./js/d3c.js"></script>
    <script type="text/javascript" src="./js/d3c_ui.js"></script>
    <script type="text/javascript" src="./js/d3c_misc.js"></script>
    <script type="text/javascript" src="./js/d3c_skills.js"></script>
    <script type="text/javascript" src="./js/select2-3.4.3/select2.min.js"></script>
    <link href="./js/select2-3.4.3/select2.css" rel="stylesheet"/>
    <style>
        tr.title {
            background-color: #cccccc;
        }
        table.stats input[type=text] {
            width: 50px;
        }
        table.stats tr>th {
            padding-right: 5px;
            text-align: right;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function () {
            if (!GLOBAL_DATA) {
                return ;
            }
            var div = $('#characters-panel>.loaded');
            div.empty();
            var sel = document.createElement('select');
            sel.addEventListener('change', changeHero.bind(sel));
            for (var i = 0; i < GLOBAL_DATA.heroes.length; i++) {
                var hero = GLOBAL_DATA.heroes[i];
                var opt = document.createElement('option');
                opt.setAttribute('value', hero.id);
                opt.appendChild(document.createTextNode(hero.name));
                sel.appendChild(opt);
            }
            div[0].appendChild(sel);
            setCurrentHeroStats();
        });
        GLOBAL_DATA = <?php echo json_encode($data); ?>;
        CURRENT_HERO = <?php echo json_encode($hero); ?>;
    </script>
<body>
    <div class="characters" id="characters-panel">
        <div>
            <form action="/index.php">
                realm : <select name="realm">
                    <option value="eu" <?php echo $realm == 'eu' ? 'selected' : '' ?>>Europe</option>
                    <option value="us" <?php echo $realm == 'us' ? 'selected' : '' ?>>United States</option>
                </select>
                BattleTag <span class="example">(for example: Astaltar#2844)</span>: <input type="text" name="battleTag" value="<?php echo $battleTag; ?>"/>
                <input type="submit" value="update">
            </form>
        </div>

        <div class="loaded">

        </div>
    </div>
	<div id="character1">
		<div style="display: inline-block; vertical-align: top">
			<table class="stats">
				<tr class="title"><td colspan="2">Character</td></tr>
				<tr>
					<th>Class</th>
					<td>
						<select name="c_class">
							<option value="barbarian">Barbarian</option>
							<option value="demon_hunter">Demon Hunter</option>
							<option value="monk">Monk</option>
							<option value="witch_doctor">Witch Doctor</option>
							<option value="wizard">Wizard</option>
						</select>
					</td>
				</tr>
				<tr>
					<th>Level</th>
					<td><input type="text" name="c_level" value="60"/></td>
				</tr>
				<tr>
					<th>Passive Skills</th>
					<td>
                        <input type="hidden" name="c_passive_skils" style="width: 400px" />
                </tr>
				<tr>
					<th>Mob level</th>
					<td><input type="text" name="c_mobLevel" value="63"/></td>
				</tr>
				<tr class="title"><td colspan="2">Base Stats</td></tr>
				<tr>
					<th>Strength</th>
					<td><input type="text" name="bs_strength" value="1063"/></td>
				</tr>
				<tr>
					<th>Dexterity</th>
					<td><input type="text" name="bs_dexterity"/></td>
				</tr>
				<tr>
					<th>Intelligence</th>
					<td><input type="text" name="bs_intelligence"/></td>
				</tr>
				<tr>
					<th>Vitality</th>
					<td><input type="text" name="bs_vitality" value="900"/></td>
				</tr>
				<tr class="title"><td colspan="2">Defense Stats</td></tr>
				<tr>
					<th>Armor</th>
					<td><input type="text" name="ds_armor" value="5067"/></td>
				</tr>
				<tr>
					<th>Bonus Health (+%)</th>
					<td><input type="text" name="ds_health_bonus" value="7"/></td>
				</tr>
				<tr>
					<th>All resistance</th>
					<td><input type="text" name="ds_allResistance" value="22"/></td>
				</tr>
	
				<tr class="title"><td colspan="2">Offense Stats</td></tr>
				<tr>
					<th>Critical Hit Chance</th>
					<td><input type="text" name="os_criticalhitchance" value="6.5"/></td>
				</tr>
				<tr>
					<th>Critical Hit Damage</th>
					<td><input type="text" name="os_criticalhitdamage" value="86"/></td>
				</tr>
				<tr>
					<th>Increase Damage By Passive Skills</th>
					<td><input type="text" name="os_increasedamagebypassiveskills"/></td>
				</tr>
                <tr>
                    <th>Attack speed</th>
                    <td>
                        <input type="text" name="os_attack_speed" value="14"/>
                    </td>
                </tr>
				<tr class="title"><td colspan="2">Weapon(s)</td></tr>
				<tr>
					<th>Main Weapon</th>
					<td>
						<select name="mw_weapon">
							<option> - select weapon - </option>
							<optgroup label="One-Handed">
								<option value="1" selected="1">Axe</option>
								<option value="2">Ceremonial knife</option>
								<option value="3">Dagger</option>
								<option value="4">First weapon</option>
								<option value="5">Mace</option>
								<option value="6">Migthy weapon</option>
								<option value="7">Spear</option>
								<option value="8">Sword</option>
							</optgroup>
							<optgroup label="Two-handed">
								<option value="16">Axe</option>
								<option value="17">Daibo</option>
								<option value="18">Mace</option>
								<option value="19">Migthy weapon</option>
								<option value="20">Polearm</option>
								<option value="21">Stave</option>
								<option value="22">Sword</option>
							</optgroup>
							<optgroup label="ranged">
								<option value="32">Bow</option>
								<option value="33">Crossbow</option>
								<option value="34">Hand crossbow</option>
								<option value="35">Wand</option>
							</optgroup>
						</select>
					</td>
				</tr>
				<tr>
					<th>Main weapon damage</th>
					<td>
						<input type="text" name="mw_mindmg" value="140"/> - <input type="text" name="mw_maxdmg" value="259"/>
					</td>
				</tr>
			</table>
			
		</div>
		<div id="divStatistics" style="display: inline-block; vertical-align: top">
			<table class="stats">
				<tr class="title"><td colspan="2">Offensive stats</td></tr>
				<tr>
					<th>DPS</th>
					<td id="tdDps"></td>
				</tr>
				<tr>
					<th>Damage</th>
					<td id="tdDamage"></td>
				</tr>
				<tr>
					<th>Attacks per second</th>
					<td id="tdAps"></td>
				</tr>
	
				<tr class="title"><td colspan="2">Offensive improvements</td></tr>
	
				<tr class="title"><td colspan="2">Defensive stats</td></tr>
				<tr>
					<th>Health points</th>
					<td id="tdHealth"></td>
				</tr>
				<tr>
					<th>Effective health points</th>
					<td id="tdEhp"></td>
				</tr>
				<tr>
					<th>Damage reduced by ARMOR</th>
					<td id="tdArmorDR"></td>
				</tr>
				<tr>
					<th>Damage reduced by RESISTANCEs</th>
					<td id="tdResistanceDR"></td>
				</tr>
				<tr>
					<th>Damage reduced by PASSIVEs</th>
					<td id="tdPassiveDR"></td>
				</tr>
				<tr>
					<th>TOTAL damage reduction</th>
					<td id="tdTotalDR"></td>
				</tr>
	
				<tr class="title"><td colspan="2">Defensive improvements</td></tr>
				<tr>
				<th><input type="text" name="ehp_armor" value="10"/> armor</th>
					<td>+<span id="lblIncreaseEhpByArmor"></span> EHP</td>
				</tr>
				<tr>
				<th><input type="text" name="ehp_resistance" value="1"/> all resistance</th>
					<td>+<span id="lblIncreaseEhpByResistance"></span> EHP</td>
				</tr>
				<tr>
				<th><input type="text" name="ehp_vitality" value="1" /> vitality</th>
					<td>+<span id="lblIncreaseEhpByVitality"></span>EHP</td>
				</tr>
				<tr>
				<th>+<input type="text" name="ehp_hbonusp" value="1"/>% life</th>
					<td>+<span id="lblIncreaseEhpByLifePercent"></span>EHP</td>
				</tr>
			</table>
		</div>
	</div>
	<div>Increase Armor</div>
    <div id="placeholder" style="width:600px;height:300px;"></div>

    <script type="text/javascript">
        dui = new d3cUI('character1');
        dui.setHero(CURRENT_HERO);
    </script>

<!-- Jquery Flot
    <script type="text/javascript">
        var options = {
            lines: { show: true },
            points: { show: true },
            xaxis: { tickDecimals: 0, tickSize: 1 }
        };
        var data = [];
        var placeholder = $("#placeholder");
        
        var weapon = new Weapon(Weapon.WT_1H_AXE);
        weapon.setIncreasesAttackSpeed(14);
        weapon.setDamage(140,259);

        var barb = new Barbarian();        
        barb.setStatStrength(1063).setCriticalHitChance(6.5).setCriticalHitDamage(86);
        barb.setWeapon(weapon);
        barb.setArmor(5061).setResistance(22).setHealth(22376);
        console.log(barb.getEffectiveHealthPoints(60));
        //plotDPS(barb, data);
        plotDR_Armor(barb, data);
        $.plot(placeholder, data, options);

        function plotDPS(character, data)
        {
            var stat = character.getMainStatValue();
            data.push({'label' : 'Current', 'data': [[stat, barb.getDPS()]], 'points': {'symbol': 'circle'}});
            var statValue = [];
            for (var i = 0.8 * stat; i < 1.2 * stat; i += stat * 0.02) {
                barb.setStatStrength(i);
                statValue.push([i, barb.getDPS()]);
            }
            data.push({'label' : 'change stat value', 'data' : statValue});
        }

        function plotDR_Armor(character, data)
        {
            var stat = character.getArmor();
            data.push({'label' : 'Current', 'data': [[stat, barb.getDamageReducedByArmor()]], 'points': {'symbol': 'circle'}});
            var statValue = [];
            for (var i = 0.8 * stat; i < 1.2 * stat; i += stat * 0.02) {
                barb.setArmor(i);
                statValue.push([i, barb.getDamageReducedByArmor()]);
            }
            data.push({'label' : 'change stat value', 'data' : statValue});
        }
    </script>
    -->
</body>
</html>
